S=input()
flag=int(input())
n=int(input())

for i in range(0,n):
    Line=input()
    line=Line
    s=S
    if flag==0:
        line=line.upper()
        s=s.upper()
    if line.find(s)>-1:
        print(Line)